<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'web_site/web_events_dispatcher.class.php');

require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


require_once($WSC->get_site_inc_dir().'_sys/site_web_page.class.php');


abstract class Site_Web_Page_WED extends Site_Web_Page {
	protected $wed;
	protected $self_call = array();


	function __construct($page_name) {
		parent::__construct($page_name);

		$this->wed = new Web_Event_Dispatcher($this);

		$this->set_default_event_action_pair();
	}


	public function run() {
		$dispatch = $this->wed->get_dispatch($this->get__context());

		$ret = call_user_func(array($this, $dispatch['target']), $dispatch['param']);

		if ($ret instanceof Web_Page_View) {
			$ext_exp = new TPLE_Exports();
			$ext_exp->add_pair('self_call', $this->self_call);

			$ret->add_exports($ext_exp);
		} else {
			throw new Tangra_Module_Exception('Returned value is not instance of Web_Page_View. Current value: '.$ret);
		}

		return $ret;
	}


	protected function set_default_event_action_pair() {
		$this->wed->set_default_event_action_pair(new Web_Event_Simple('default', Tangra_Parameter_Method::NONE, ''), 'default_event');
	}


	public function add_self_call_get_param($param, $value) {
		if (!array_key_exists($param, $this->self_call)) {
			$this->self_call[$param] = $value;
		} else {
			throw new TE_Key_Already_Exists($param);
		}
	}


	abstract protected function default_event();
}


